package User;

import book.Book;
import book.Libary;
import book.PairOfUidAndBookId;
import utils.AnalyingBorrowBook;
import utils.LibarySingleton;
import utils.ScannerSingleton;

import java.io.IOException;
import java.util.Scanner;
import constant.constant;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 殇
 * Date: 2025-07-16
 * Time: 16:20
 */
public class NormalUser extends User{
    private Scanner scanner;
    //User实际借阅次数
    private int borrowCount;
    //借阅书籍的信息数组
    private PairOfUidAndBookId[] borrowBooks;
    //借阅书籍分析工具
    private  AnalyingBorrowBook analyingBorrowBook=new AnalyingBorrowBook();

    private Libary libary;

    public NormalUser(String name, int userId) {

        super(name, userId,"普通用户");
        scanner=ScannerSingleton.getScanner();
        libary= LibarySingleton.getLibary();
        loadBooks();

    }

    //读取借阅书籍信息
    public void loadBooks() {
        PairOfUidAndBookId[] tmpBooks;
        tmpBooks = analyingBorrowBook.loadObject(constant.BORROW_BOOK_FILE_NAME);
        borrowBooks=new PairOfUidAndBookId[constant.BORROW_BOOK_MAX_NUM];
        if (tmpBooks == null)
            borrowCount = 0;
        else {
            int len = tmpBooks.length;
            if (len > constant.BORROW_BOOK_MAX_NUM) {
                borrowBooks = new PairOfUidAndBookId[len];
            }
            for (int i = 0; i < len; i++) {
                //if(信息.userdId==this.userId)
                borrowBooks[i] = tmpBooks[i];
            }
            borrowCount = len;
        }
    }

    //存储书籍信息
    public void storeBooks(){
        analyingBorrowBook.storeObject(borrowBooks,constant.BORROW_BOOK_FILE_NAME);
    }


    //普通人员菜单
    @Override
    public int display() {
        System.out.println("普通⽤⼾ " + name + " 的操作菜单:");
        System.out.println("1. 查找图书");
        System.out.println("2. 打印所有的图书");
        System.out.println("3. 退出系统");
        System.out.println("4. 借阅图书");
        System.out.println("5. 归还图书");
        System.out.println("6. 查看当前个⼈借阅情况");
        System.out.println("请选择你的操作：");
        return scanner.nextInt();
    }



    //借阅图书
    public void borrowBook() {
        libary.display();
        System.out.println("请输入借阅的书籍编号:");
        int bookId=scanner.nextInt();
        scanner.nextLine();

        //检查是否存在书籍
        if(libary.getBookCount()==0){
            System.out.println("当前没有书籍，无法借阅");
        }

        //加载借阅书籍信息
        loadBooks();

        //检查bookId是否合法
        Book book=libary.searchBookId(bookId);
        if(book==null){
            System.out.println("没有符合条件的书籍!!!");
        }

        //判断该书籍是否已经被借出
        for(int i=0;i<borrowCount;i++){
            PairOfUidAndBookId pairOfUidAndBookId=borrowBooks[i];
            if(pairOfUidAndBookId.getBookId()==book.getBookId()){
               if(pairOfUidAndBookId.getUserId()==userId){
                   System.out.println("您已经借阅过该书籍");
                   return ;
               }
               else{
                   System.out.println("该书籍已被别人借出,用户Id是:"+pairOfUidAndBookId.getUserId());
                   return ;
               }
            }
        }

        //未被借出，则借用
        libary.borrowBook(bookId);


        //将借阅信息写入借阅数组
        PairOfUidAndBookId pairOfUidAndBookId=new PairOfUidAndBookId(userId,book.getBookId());
        borrowBooks[borrowCount++]=pairOfUidAndBookId;
        //将借阅数组信息写入文件中
        storeBooks();
        System.out.println("借阅成功!!!");

    }
    //归还图书
    public void returnBook() {
        loadBooks();
        if(borrowCount==0){
            System.out.println("当前没有用户借出过书籍");
        }
        libary.display();
        System.out.println("请输入要归还书籍的Id:");
        int bookId=scanner.nextInt();

        Book book=libary.searchBookId(bookId);
        if(book==null){
            System.out.println("您输入的Id不合法");
        }

        //查找借阅的书籍
        for(int i=0;i<borrowCount;i++){
            if(book.getBookId()==borrowBooks[i].getBookId()){
                if(borrowBooks[i].getUserId()!=userId){
                    System.out.println("您不是该书籍的借阅人，无法借阅");
                }
                else{
                    //当是自己借的，删除该位置的借阅信息
                    //用最后一个元素来覆盖该位置，也可以将后面的元素依次往前覆盖(arr[i]=arr[i+1])
                    libary.returnBook(bookId);
                    borrowBooks[i]=borrowBooks[borrowCount-1];
                    borrowBooks[borrowCount-1]=null;
                    borrowCount--;
                    storeBooks();
                    System.out.println("归还成功!!!");
                }
                return ;
            }
        }
        System.out.println("您未借阅过该书籍");

    }
    //查看个⼈借阅情况
    public void viewBorrowHistory() {
        loadBooks();
        if (borrowCount == 0) {
            System.out.println("您未借阅过书籍");
        }
        boolean flg = false;
        System.out.println("您的借阅信息如下:");
        //遍历数组，userId对应的书籍
        for (int i = 0; i < borrowCount; i++) {
            if (userId == borrowBooks[i].getUserId()) {
                flg = true;
                Book book = libary.searchBookId(borrowBooks[i].getBookId());
                System.out.println(book);
            }
        }
        if (flg == false) {
            System.out.println("您未借阅过书籍");
        }
    }
}
